package com.lucun.es_transaction.service.impl;

import com.lucun.es_transaction.entity.Authentication;
import com.lucun.es_transaction.entity.User;
import com.lucun.es_transaction.entity.UserInfo;
import com.lucun.es_transaction.entity.UserLog;
import com.lucun.es_transaction.mapper.AuthenticationMapper;
import com.lucun.es_transaction.mapper.UserInfoMapper;
import com.lucun.es_transaction.mapper.UserLogMapper;
import com.lucun.es_transaction.mapper.UserMapper;
import com.lucun.es_transaction.service.AuthenticationService;
import com.lucun.es_transaction.util.BaseException;
import com.lucun.es_transaction.util.JsonResult;
import com.lucun.es_transaction.util.JwtUtil;
import com.lucun.es_transaction.util.RegexValidateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AuthenticationServiceImpl implements AuthenticationService {
    @Autowired
    AuthenticationMapper authenticationMapper;
    @Autowired
    UserLogMapper logMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public JsonResult<?> insert(Authentication authentication, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(authentication)){
            throw new BaseException("数据为空");
        }
        String tokenNumber = JwtUtil.getNumberByToken(request);
        if (StringUtils.isBlank(tokenNumber)){
            throw new BaseException("解析number失败");
        }
        String userName = authentication.getUserName();
        if (StringUtils.isBlank(userName)){
            throw new BaseException("没有获取姓名");
        }
        if (userName.length()<0|| userName.length()>10){
            throw new BaseException("姓名长度不得小于0大于10字符");
        }
        String card = authentication.getUserCard();
        if (StringUtils.isBlank(card)){
            throw new BaseException("没有获取身份证号码");
        }
        boolean userCard = new RegexValidateUtil().isIDNumber(card);
        if (!userCard){
            throw new BaseException("填写正确的身份证号码");
        }
//        获取登录的number值赋值给实名认证number
        authentication.setNumber(tokenNumber);
//        根据登录的number值 获取到此用户个人信息的id
        UserInfo userInfo1 = userInfoMapper.findByNumber(tokenNumber);
        String j = userInfo1.getNumber();
        if (StringUtils.isBlank(j)){
            throw new BaseException("没有此用户信息，需要填写个人信息");
        }
        /* 添加成功 后修改个人信息表的认证状态 */
        Integer realname = userInfo1.getId();
        if (ObjectUtils.isEmpty(realname)){
            throw new BaseException("没有获取id");
        }
        userInfoMapper.update(realname);
//        添加成功后 解析number 更改role
        String number = JwtUtil.getNumberByToken(request);
        if (StringUtils.isBlank(number)){
            throw new BaseException("解析number失败");
        }
        Authentication authentication1 = new Authentication();
        authentication1.setNumber(authentication.getNumber());
        Authentication userName1 = authenticationMapper.selectUserByAll(authentication1);
        if(!ObjectUtils.isEmpty(userName1)){
            throw new BaseException("该用户名已被认证");
        }
        authentication.setCreateTime(new Date());
        /* 图片上传 */
//        ImageUtil imageUtil = new ImageUtil();
//        authentication.setCardJust(imageUtil.getUploadFile());
        int i = authenticationMapper.insert(authentication);
//        认证信息添加成功则生成日志
        int idByToken = JwtUtil.getIdByToken(request);
        if (ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        UserLog log = new UserLog();
        String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte) 1);
        log.setUserId(idByToken);
        if(i>0){
            log.setLogName(tokenNumber+"在"+date+"认证成功"+ authentication.getUserName()+"用户");
            log.setLogStatus((byte) 1);
            logMapper.insertSelective(log);
        }else{
            log.setLogName(tokenNumber+"在"+date+"认证失败"+ authentication.getUserName()+"用户");
            log.setLogStatus((byte) 0);
            logMapper.insertSelective(log);
        }
        if (i>0) {
            return new JsonResult<>("200", "认证成功", authentication);
        }else {
            return new JsonResult<>("500", "认证失败", authentication);
        }
    }
/*
* 删除
* */
    @Override
    public JsonResult<?> delete(Authentication authentication, HttpServletRequest request) {
        int id = authentication.getId();
        if (ObjectUtils.isEmpty(id)) {
            throw new BaseException("没有获取id");
        }
        int idtoken = JwtUtil.getIdByToken(request);
        if (ObjectUtils.isEmpty(idtoken)) {
            throw new BaseException("解析token失败");
        }
        int i = authenticationMapper.deleteByPrimaryKey(id);
        UserLog log = new UserLog();
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte) 2);
        log.setUserId(idtoken);
        if (i > 0) {
            log.setLogName(idtoken + "在" + date + "删除成功" + authentication.getUserName() + "用户");
            log.setLogStatus((byte) 1);
            logMapper.insertSelective(log);
        } else {
            log.setLogName(idtoken + "在" + date + "删除失败" + authentication.getUserName() + "用户");
            log.setLogStatus((byte) 0);
            logMapper.insertSelective(log);
        }
        if (i > 0) {
            return new JsonResult<>("200", "删除认证成功", authentication);
        } else {
            return new JsonResult<>("500", "删除失败", authentication);
        }
    }
}
